package cn.zwx.distributed.svc.fallback;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.enums.ResponseCodeEnum;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.distributed.svc.DistributedServiceApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @author zhangwenxue
 */
@Component
public class DistributedApiFallback implements FallbackFactory<DistributedServiceApi> {
    private static final Logger logger = LoggerFactory.getLogger(DistributedApiFallback.class);
    @Override
    public DistributedServiceApi create(Throwable cause) {
        return new DistributedServiceApi() {
            @Override
            public DistributedApiFallback create(Throwable cause) {
                logger.error("调用Distributed-Service fallback",cause);
                return null;
            }

            @Override
            public ResponseEntity<String> getDistributedId() throws BizWrapException {
                logger.error("getDistributedId fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public ResponseEntity<String> parsDistributedId(String distributedId) throws BizWrapException {
                logger.error("parsDistributedId distributedId:{},fallback",distributedId,cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

        };
    }
}
